package com.scorpio.api;

import com.scorpio.appuser.domain.AppUserEntity;
import com.scorpio.appuser.domain.UserAddressEntity;
import com.scorpio.appuser.domain.UserInvoiceEntity;
import com.scorpio.appuser.service.AppUserService;
import com.scorpio.appuser.service.UserAddressService;
import com.scorpio.appuser.service.UserInvoiceService;
import com.scorpio.basic.vo.OperationIdVo;
import com.scorpio.common.utils.verification.UpdateGroup;
import com.scorpio.core.controller.BaseController;
import com.scorpio.core.domain.Result;
import com.scorpio.framework.annotation.Log;
import com.scorpio.framework.annotation.PersonalLogin;
import com.scorpio.framework.annotation.PersonalLoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController extends BaseController {

    @Autowired
    private AppUserService appUserService;


    @Autowired
    private UserAddressService addressService;

    @Autowired
    private UserInvoiceService userInvoiceService;

    @Log(title = "获取用户详情")
    @PersonalLogin
    @GetMapping("details")
    public Result details(@PersonalLoginUser AppUserEntity user) {
        return Result.success(appUserService.details(user.getId()));
    }

    @Log(title = "添加地址")
    @PersonalLogin
    @PostMapping("addAddress")
    public Result addAddress(@PersonalLoginUser AppUserEntity user, @Validated @RequestBody UserAddressEntity entity) {
        entity.setId(null);
        entity.setUid(user.getId());
        return Result.success(addressService.addAddress(entity));
    }

    @Log(title = "更新地址")
    @PersonalLogin
    @PostMapping("updateAddress")
    public Result updateAddress(@PersonalLoginUser AppUserEntity user, @RequestBody UserAddressEntity entity) {
        entity.setUid(user.getId());
        if (entity.getId() == null) {
            return Result.success(addressService.addAddress(entity));
        } else {
            return Result.success(addressService.updateAddress(entity));
        }
    }

    @Log(title = "删除地址")
    @PersonalLogin
    @PostMapping("deleteAddress")
    public Result deleteAddress(@PersonalLoginUser AppUserEntity user, @Validated @RequestBody OperationIdVo vo) {
        return toResult(addressService.deleteAddress(user.getId(), vo.getId()));
    }

    @Log(title = "地址列表")
    @PersonalLogin
    @GetMapping("addressList")
    public Result addressList(@PersonalLoginUser AppUserEntity user) {
        return Result.success(addressService.appList(user.getId()));
    }

    @Log(title = "保存发票信息")
    @PersonalLogin
    @PostMapping("saveInvoice")
    public Result saveInvoice(@PersonalLoginUser AppUserEntity user, @Validated @RequestBody UserInvoiceEntity entity) {
        entity.setUid(user.getId());
        return Result.success(userInvoiceService.save(entity));
    }

    @Log(title = "发票详情信息")
    @PersonalLogin
    @GetMapping("detailsInvoice")
    public Result detailsInvoice(@PersonalLoginUser AppUserEntity user) {
        return Result.success(userInvoiceService.selectByUser(user.getId()));
    }

}
